import { ref, reactive } from 'vue'
import { useStore } from '@/Pinia/index'
import { nowDate } from "@/utlis/time"
import * as $http from '@/utlis/interface/Wode'

interface User {
  name: string
  idCard: any
  headFace: string
  reverseCard: string
}

const frontView = reactive<any>([]) //身份证正面
const rearView = reactive<any>([]) //身份证背面
export default (proxy: any) => {
  const SkipModel = ref<boolean>(false)
  const User = reactive<User>({
    name: '',
    idCard: '',
    headFace: '',
    reverseCard: '',
  })
  const fileList = reactive<any>([])
  //上传身份证图片正面
  const idCardFrontAfterRead = (event: any): void => {
    User.headFace = ''
    let headFaceImg = event.file.url
    uni.showLoading({
      title: '正在上传',
    })
    uni.uploadFile({
      url: 'https://api.waimaishi.cn/app-api/common/AppUpload',
      header: {
        Authorization: uni.getStorageSync('Authorization'),
      },
      formData: {
        pathPrefix: 'rider' + nowDate()
      },
      filePath: event.file.url, //当前缓存
      name: 'file', //二进制文件
      success: (uploadFileRes: any) => {
        uni.hideLoading()
        let imgData = JSON.parse(uploadFileRes.data)
        User.headFace = imgData.data
        proxy.$API(proxy).Toast('上传成功', 'success')
      },
      fail: (err) => {
        uni.hideLoading()
        proxy.$API(proxy).Toast('上传失败', 'error')
      },
    })
  }
  //上传图片背面
  const idCardBackAfterRead = (event: any): void => {
    User.reverseCard = ''
    uni.showLoading({
      title: '正在上传',
    })
    let reverseCard = event.file.url
    uni.showLoading({
      title: '正在上传',
    })
    uni.uploadFile({
      url: 'https://api.waimaishi.cn/app-api/common/AppUpload',
      header: {
        Authorization: uni.getStorageSync('Authorization'),
      },
      formData: {
        pathPrefix: 'rider' + nowDate()
      },
      filePath: event.file.url, //当前缓存
      name: 'file', //二进制文件
      success: (uploadFileRes: any) => {
        uni.hideLoading()
        let imgData = JSON.parse(uploadFileRes.data)
        User.reverseCard = imgData.data
        proxy.$API(proxy).Toast('上传成功', 'success')
      },
      fail: (err) => {
        uni.hideLoading()
        proxy.$API(proxy).Toast('上传失败', 'error')
      },
    })
  }
  // 提交
  const Submit = (): void => {
    let nameText = /^[\u4e00-\u9fa5]{2,6}$/
    if (!nameText.test(User.name.trim())) {
      return proxy.$API(proxy).Toast('请输正确的姓名', 'error')
    }
    let idCardReg =
      /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/

    if (!idCardReg.test(User.idCard.trim())) {
      return proxy.$API(proxy).Toast('请输入正确的身份证号码', 'error')
    }
    if (User.headFace == '') {
      return proxy.$API(proxy).Toast('请上传正面照', 'error')
    }
    if (User.reverseCard == '') {
      return proxy.$API(proxy).Toast('请上传返面照', 'error')
    }
    uni.showLoading({
      title: '提交中',
    })
    $http.submitMyInfo(User, proxy).then((res: any) => {
      if (res.code == 0) {
        const realyStatus = useStore()
        uni.hideLoading()
        proxy.$API(proxy).Toast(res.msg, 'success')
        setTimeout(() => {
          realyStatus.infoData.userRealNameStatus = 1
          uni.redirectTo({
            url: '/pages/Home/Home'
          })

        }, 1000)
      }
    }).catch((err: any) => {
      uni.hideLoading()
      proxy.$API(proxy).Toast(err.msg, 'error')
    })
  }
  // 跳过模态框确定
  const confirmSkipModel = (): void => {
    uni.redirectTo({
      url: '/pages/Home/Home'
    })
  }
  return {
    rearView,
    frontView,
    SkipModel,
    User,
    idCardFrontAfterRead,
    idCardBackAfterRead,
    Submit,
    confirmSkipModel,
  }
}
